package com.easygem.business.service.imp;

import com.easygem.business.converter.SysNoticeConverter;
import com.easygem.business.mapper.SysNoticeMapper;
import com.easygem.business.service.SysNoticeService;
import com.easygem.business.service.base.AbstractServiceImpl;
import com.easygem.common.error.SystemCodeEnum;
import com.easygem.common.error.SystemException;
import com.easygem.common.model.business.SysNotice;
import com.easygem.common.response.ActiveUser;
import com.easygem.common.vo.business.SysNoticeVO;
import com.easygem.common.vo.system.PageVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 文晓平
 * @version 1.0
 * @Description 系统通知的业务层实现类
 * @date 2024/12/21 0:32
 */
@Service
public class SysNoticeServiceImpl extends AbstractServiceImpl<SysNoticeMapper, SysNotice, Integer> implements SysNoticeService {
    @Autowired
    private SysNoticeMapper sysNoticeMapper;

    @Override
    public PageVO<SysNotice> findList(Integer pageNum, Integer pageSize, SysNoticeVO sysNoticeVO) {
        PageHelper.startPage(pageNum,pageSize);
        SysNotice sysNotice = new SysNotice();
        sysNotice.setUserName(sysNoticeVO.getUserName());
        sysNotice.setTitle(sysNoticeVO.getTitle());

        List<SysNotice> sysNoticeList = sysNoticeMapper.selectSysNoticeList(sysNotice);
        System.out.println("*************************"+sysNoticeList);

        PageInfo<SysNotice> info=new PageInfo<>(sysNoticeList);
        return new PageVO<>(info.getTotal(),sysNoticeList);

    }

    /**
     * 添加系统通知
     *
     * @param sysNoticeVO
     */
    @Override
    public void add(SysNoticeVO sysNoticeVO) {
        SysNotice sysNotice = new SysNotice();
        //获取当前系统时间，时间戳
        sysNoticeVO.setCreateTime(new Timestamp(System.currentTimeMillis()));
        //获得到登录后的用户信息，通用方式
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        sysNoticeVO.setUserId(activeUser.getEmployee().getEmpId());
        sysNoticeVO.setIsRead(0);//未读
        BeanUtils.copyProperties(sysNoticeVO,sysNotice);
        sysNoticeMapper.insert(sysNotice);
        if (sysNoticeVO.getIsScope()==1){//当发布范围是全部时，将数据添加到前台的系统通知
            sysNoticeMapper.insertSysNotice(sysNotice);
        }
    }

    /**
     * 编辑通知
     *
     * @param id
     * @return
     */
    @Override
    public SysNoticeVO edit(Integer id) throws SystemException {
        SysNotice sysNotice = sysNoticeMapper.selectByPrimaryKey(id);
        if(sysNotice==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"编辑的系统通知不存在");
        }
        SysNoticeVO sysNoticeVO = SysNoticeConverter.converterToSysNoticeVO(sysNotice);
        return sysNoticeVO;
    }

    /**
     * 更新通知
     *
     * @param id
     * @param sysNoticeVO
     */
    @Override
    public void update(Integer id, SysNoticeVO sysNoticeVO) throws SystemException {
        SysNotice dbSysNotice = sysNoticeMapper.selectByPrimaryKey(id);
        if(dbSysNotice==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"编辑的系统通知不存在");
        }
        SysNotice sysNotice = new SysNotice();
        BeanUtils.copyProperties(sysNoticeVO,sysNotice);
        sysNotice.setNoticeId(id);
        sysNoticeMapper.updateByPrimaryKeySelective(sysNotice);
    }

    /**
     * 删除通知
     * @param id
     */
    @Override
    public void delete(Integer id) throws SystemException {
        SysNotice sysNotice = sysNoticeMapper.selectByPrimaryKey(id);
        if(sysNotice==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"要删除的系统通知不存在");
        }
        sysNoticeMapper.deleteByPrimaryKey(id);
        sysNoticeMapper.deleteSysNoticeIsByNoticeId(id);//删除是否已读系统通知
    }

    /**
     * 删除多个通知
     * @param map 多个id
     * @throws SystemException
     */
    @Override
    public void deleteMore(Map<String, Object> map) throws SystemException {
        sysNoticeMapper.deleteMore(map);
        sysNoticeMapper.deleteSysNoticeIsByNoticeIdMap(map);
    }

    /**
     * 所有系统通知
     *
     * @return
     */
    @Override
    public List<SysNoticeVO> findAllVO() {
        List<SysNotice> sysNotices = sysNoticeMapper.selectAll();
        //转vo
        List<SysNoticeVO> sysNoticeVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(sysNotices)) {
            for (SysNotice sysNotice : sysNotices) {
                SysNoticeVO s = new SysNoticeVO();
                BeanUtils.copyProperties(sysNotice, s);
                Example o = new Example(SysNotice.class);
                o.createCriteria().andEqualTo("noticeId",sysNotice.getNoticeId());
                sysNoticeVOS.add(s);
            }
        }
        return sysNoticeVOS;
    }

    /**
     * 全部系统信息
     *
     * @return
     */
    @Override
    public List<SysNotice> findAll() {
        return sysNoticeMapper.selectAll();
    }

    /**
     * 根据用户id查询出系统通知
     *
     * @return
     */
    @Override
    public List<SysNoticeVO> selectByUserId() {
        //获得到登录后的用户信息，通用方式
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        List<SysNoticeVO> sysNoticeVOList=new ArrayList<>();
        List<SysNotice> sysNotices=sysNoticeMapper.selectByUserId(activeUser.getEmployee().getEmpId());
        for (SysNotice sysNotice : sysNotices) {
            sysNoticeVOList.add(SysNoticeConverter.converterToSysNoticeVO(sysNotice));
        }
        return sysNoticeVOList;
    }

    /**
     * 添加是否已读系统通知
     * @param noticeId 系统通知id
     * @return
     */
    @Override
    public int addSysNoticeIs(Long noticeId) {
        //获得到登录后的用户信息，通用方式
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        return sysNoticeMapper.addSysNoticeIs(activeUser.getEmployee().getEmpId(), noticeId);
    }
}




